package com.bazinga.contact.view;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.bazinga.contact.model.SingleJob;
import com.opensymphony.xwork2.ActionSupport;

public class SearchJobsAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private static final String dbServer = "127.0.0.1";
	@SuppressWarnings("unused")
	private Map<String, Object> session;
	private String skills = null;
	public String getSkills() {
		return skills;
	}
	public void setSkills(String skills) {
		this.skills = skills;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public List<SingleJob> getSearchResults() {
		return searchResults;
	}
	public void setSearchResults(List<SingleJob> searchResults) {
		this.searchResults = searchResults;
	}
	private String title = null;
	private String city = null;
	private String state = null;
	private List<SingleJob> searchResults;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	/**
     * @see HttpServlet#HttpServlet()
     */
	
	public String execute() {
		System.out.println(city);
		System.out.println(state);
		System.out.println(title);
		System.out.println(skills);
		String dbUrl = "jdbc:mysql://" + dbServer + ":3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String searchQuery = "select * from jobdetails where title like '%" + title + "%' OR city='" + city +"' OR state='" + state + "';";
		
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		
		try {
			rs = stmt.executeQuery(searchQuery);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}
		
		searchResults = new ArrayList<SingleJob>();
		try {
			while(rs.next()) {
				SingleJob temp = new SingleJob();
				temp.setJobid(rs.getString("jobid"));
				temp.setSalary(rs.getString("salary"));
				temp.setCity(rs.getString("city"));
				temp.setTitle(rs.getString("title"));
				searchResults.add(temp);
			}
		}
		
		catch(SQLException ex) {
			ex.printStackTrace();
		}
		
		catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return "success";
	}
    public SearchJobsAction() {
        super();
        // TODO Auto-generated constructor stub
    }

}
